package com.iwhalecloud.bss.kite.cucc.manager.scene.service.query;

import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.manager.enums.InstType;
import com.iwhalecloud.bss.kite.manager.enums.RelType;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInst;
import com.iwhalecloud.bss.kite.manager.inst.graph.IInstRelGraph;
import com.iwhalecloud.bss.kite.manager.inst.graph.impl.InstVertex;
import com.iwhalecloud.bss.kite.manager.scene.util.InstRelGraphUtils;
import com.iwhalecloud.bss.kite.manager.scene.util.InstUtils;
import com.iwhalecloud.bss.kite.manager.scene.vertex.ISubOfferInst;
import com.iwhalecloud.bss.kite.manager.transaction.TransactionContext;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 可选包销售品实例查询类
 */
@Service("manager.localQueryAdditionalOfferInstService")
public class LocalQueryAdditionalOfferInstService {

    /**
     * 获取套餐销售品实例的所有可选包销售品实例
     * @param custId
     * @param offerInstId
     * @return
     */
    public List<KiteOfferInst> listAdditionalOfferInstsFromMemOfferInst(String custId, String offerInstId) {
        IInstRelGraph custInstRelGraph = TransactionContext.getContainer().getCustInstRelGraph(custId);
        InstVertex instVertex = InstRelGraphUtils.getCustInstVertex(custId, InstType.MEM_OFFER_INST, offerInstId);
        if (Objects.isNull(instVertex)) {
            ErrorConsts.OFFER_INST_NOT_EXIST.throwOut(offerInstId, custId);
        }

        if (instVertex instanceof ISubOfferInst) {
            ((ISubOfferInst) instVertex).listSubOfferInsts(custInstRelGraph);
        }

        List<InstVertex> additionalOfferInstVertexs = InstRelGraphUtils.listSubInstVertexs(custInstRelGraph, instVertex,
            RelType.OFFER_REL_TYPE_100000, InstType.ADDITIONAL_OFFER_INST);
        return InstUtils.transformInsts(custId, additionalOfferInstVertexs);
    }

}
